/*
** Copyright (C) 2009 Joshua Brent Marsh <joshua@icub3d.com>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 3 of the License, or
** (at your option) any later version.
**  
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**  
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**  
*/

/* $Id: test_map.c 32 2009-10-28 22:51:19Z joshua@icub3d.com $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <CUnit/Basic.h>

#include "../src/map.h"

#include "test_map.h"

Map *m, *n;

int init_test_map_suite ()
{
  m = map_create ();
  if (m == NULL)
    return 1;

  n = map_create ();
  if (n == NULL)
    {
      map_destroy (n);
      return 1;
    }

  return 0;
}

int clean_test_map_suite ()
{
  map_destroy (m);
  map_destroy (n);

  return 0;
}

void test_map_insert ()
{
  CU_ASSERT (map_insert (m, "release", "test") == 1);
  CU_ASSERT (map_insert (m, "release", "stage") == 1);
  CU_ASSERT (map_insert (m, "release", "production") == 1);

  CU_ASSERT (map_insert (m, "product", "ADT Library") == 1);
  CU_ASSERT (map_insert (m, "system", "Linux") == 1);
  CU_ASSERT (map_insert (m, "greeting", "Hi!") == 1);
  CU_ASSERT (map_insert (m, "removed", "there!") == 1);


  CU_ASSERT (map_insert (n, "product", "Icub3d's ADT Library") == 1);
  CU_ASSERT (map_insert (n, "product", "Icub3d's ADT Library") == 1);
  CU_ASSERT (map_insert (n, "product", "Icub3d's ADT Library") == 1);
  CU_ASSERT (map_insert (n, "product", "Icub3d's ADT Library") == 1);
  CU_ASSERT (map_insert (n, "system", "Ubuntu Linux") == 1);
  CU_ASSERT (map_insert (n, "greeting", "Hello!") == 1);
  CU_ASSERT (map_insert (n, "salutation", "Good bye!") == 1);
}

void test_map_get ()
{
  CU_ASSERT (strcmp (map_get (m, "release"), "production") == 0);
  CU_ASSERT (strcmp (map_get (m, "product"), "ADT Library") == 0);
  CU_ASSERT (strcmp (map_get (m, "system"), "Linux") == 0);
  CU_ASSERT (map_get (m, "nothere") == NULL);
  CU_ASSERT (map_get (m, "nor this") == NULL);


  CU_ASSERT (strcmp (map_get (n, "product"), "Icub3d's ADT Library") == 0);
  CU_ASSERT (strcmp (map_get (n, "system"), "Ubuntu Linux") == 0);
  CU_ASSERT (map_get (n, "nothere") == NULL);
  CU_ASSERT (map_get (n, "nor this") == NULL);
}

void test_map_remove ()
{
  CU_ASSERT (map_remove (n, "product") == 1);
  CU_ASSERT (map_remove (m, "removed") == 1);

  CU_ASSERT (map_remove (n, "fail") == 0);
  CU_ASSERT (map_remove (m, "boat") == 0);
}

void test_map_exists ()
{
  CU_ASSERT (map_exists (m, "release") == 1);
  CU_ASSERT (map_exists (m, "product") == 1);
  CU_ASSERT (map_exists (m, "system") == 1);
  CU_ASSERT (map_exists (m, "nope") == 0);
  CU_ASSERT (map_exists (m, "nopers") == 0);

  CU_ASSERT (map_exists (n, "release") == 0);
  CU_ASSERT (map_exists (n, "product") == 0);
  CU_ASSERT (map_exists (n, "system") == 1);
  CU_ASSERT (map_exists (n, "nope") == 0);
  CU_ASSERT (map_exists (n, "nopers") == 0);
}

void test_map_union ()
{
  CU_ASSERT (map_union (m, n) == 1);

  CU_ASSERT (strcmp (map_get (m, "product"), "ADT Library") == 0);
  CU_ASSERT (strcmp (map_get (m, "system"), "Ubuntu Linux") == 0);
  CU_ASSERT (strcmp (map_get (m, "greeting"), "Hello!") == 0);
  CU_ASSERT (strcmp (map_get (m, "release"), "production") == 0);
  CU_ASSERT (map_get (m, "releasex") == NULL);
}

int make_map_suite (CU_pSuite s)
{
  s = CU_add_suite ("Test Map Suite", 
		    init_test_map_suite, 
		    clean_test_map_suite);
  if (s == NULL)
    {
      return CU_get_error();
    }

  if (CU_add_test (s, "test map_insert's", test_map_insert) == NULL ||
      CU_add_test (s, "test map_get's", test_map_get) == NULL ||
      CU_add_test (s, "test map_remove's", test_map_remove) == NULL ||
      CU_add_test (s, "test map_exists's", test_map_exists) == NULL ||
      CU_add_test (s, "test map_union's", test_map_union) == NULL
      )
    {
      return CU_get_error();
    }

  return CU_get_error();
}

